export function getCurTimeObj() {
  let curDate = new Date()
  let year = curDate.getFullYear()
  let month = curDate.getMonth() + 1
  let date = curDate.getDate()
  let hours = curDate.getHours()
  let minutes = curDate.getMinutes()
  let weekend = curDate.getDay()
  let weeks = new Array('日', '一', '二', '三', '四', '五', '六')
  let day = `星期${weeks[weekend]}`
  // if (month<10) {
  //     month = '0'+month
  // }
  // if (date<10) {
  //     date = '0'+date
  // }
  if (hours < 10) {
    hours = '0' + hours
  }
  if (minutes < 10) {
    minutes = '0' + minutes
  }

  return {
    date: `${year}年${month}月${date}日`,
    day,
    time: `${hours}:${minutes}`
  }
}

/**
 * @param {Function} func
 * @param {number} wait
 * @param {boolean} immediate
 * @return {*}
 */
export function debounce(func, wait, immediate) {
  let timeout, args, context, timestamp, result

  const later = function() {
    // 据上一次触发时间间隔
    const last = +new Date() - timestamp

    // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
    if (last < wait && last > 0) {
      timeout = setTimeout(later, wait - last)
    } else {
      timeout = null
      // 如果设定为immediate===true，因为开始边界已经调用过了此处无需调用
      if (!immediate) {
        result = func.apply(context, args)
        if (!timeout) context = args = null
      }
    }
  }

  return function(...args) {
    context = this
    timestamp = +new Date()
    const callNow = immediate && !timeout
    // 如果延时不存在，重新设定延时
    if (!timeout) timeout = setTimeout(later, wait)
    if (callNow) {
      result = func.apply(context, args)
      context = args = null
    }

    return result
  }
}

/**
 * 钱格式化
 * @param {*} val
 */
export function moneyFormat(val, fixed = 1) {
  val = +val
  let unit = '万元'
  if (val >= 10000 || val <= -10000) {
    val = val / 10000
    unit = '亿元'
  }
  return {
    val: parseFloat(val.toFixed(fixed)),
    unit
  }
}

export const colors = [
  '#316395',
  '#994499',
  '#22aa99',
  '#aaaa11',
  '#6633cc',
  '#e67300',
  '#8b0707',
  '#651067',
  '#329262',
  '#5574a6',
  '#3b3eac',
  '#ff9900',
  '#109618',
  '#990099',
  '#0099c6',
  '#dd4477',
  '#66aa00',
  '#b82e2e'
]
